<%@page import="com.liferay.portal.kernel.util.PropsKeys"%>
<%@page import="com.liferay.portal.kernel.util.PropsUtil"%>
<%@page import="com.liferay.portal.model.PortletConstants"%>
<%@ include file="/html/init.jsp" %>

<%@page import="com.slayer.service.InvitationLocalServiceUtil"%>

<% 
	String emailAddress = StringPool.BLANK;
	long invitationId = GetterUtil.getLong(PortalUtil.getOriginalServletRequest(request).getParameter("invitationId"), 0l);
	if (invitationId > 0l) {
		emailAddress = InvitationLocalServiceUtil.getInviteeEmail(invitationId);
	}
	long inviterId = GetterUtil.getLong(PortalUtil.getOriginalServletRequest(request).getParameter("inviterId"), 0l);
	
	boolean org = GetterUtil.getBoolean(PortalUtil.getOriginalServletRequest(request).getParameter("org"), false);
%>

<portlet:actionURL var="registerURL" name="register" />

<aui:form action="<%= registerURL %>">

	<aui:input name="invitationId" type="hidden" value="<%= String.valueOf(invitationId) %>" />
	<aui:input name="inviterId" type="hidden" value="<%= String.valueOf(inviterId) %>" />

	<liferay-ui:header title="account-information"/>
	
	<aui:row>
		<aui:column>
			<aui:input autoFocus="true" name="firstName" required="<%= true %>" />
		</aui:column>
		
		<aui:column>
			<aui:input name="lastName" />
		</aui:column>		
	</aui:row>
	
	<aui:row>
		<aui:column>
			<aui:input name="emailAddress" required="<%= true %>" value="<%= emailAddress %>">
				<aui:validator name="email"/>
				<aui:validator name="custom" errorMessage="email-already-exists">
					function() {
						return emailNotExists();					
					}
				</aui:validator>				
			</aui:input>
		</aui:column>
		
		<aui:column>
			<aui:input name="mobileNumber" prefix="+091" cssClass="width-80"/>
		</aui:column>		
	</aui:row>
	
	<aui:row>
		<aui:column>
			<aui:select label="gender" name="male" showEmptyOption="true" required="true">
				<aui:option label="male" value="true" />
				<aui:option label="female" value="false" />
			</aui:select>
		</aui:column>
		
		<aui:column>
			<aui:input name="dateOfBirth"/>
		</aui:column>		
	</aui:row>
	
	<c:if test="<%= org %>">
		<aui:input name="org" type="hidden" value="<%= org %>"/>
		<liferay-ui:header title="organization-information"/>
		
		<aui:row>
			<aui:column>
				<aui:input name="organizationName" required="true" />
			</aui:column>
			
			<aui:column>
				<aui:input name="website" prefix="<%= IConstants.HTTP_PREFIX %>" cssClass="width-80"/>
			</aui:column>			
		</aui:row>
		
		<aui:row>
			<aui:column>
				<aui:input name="subdomain" prefix="<%= IConstants.HTTP_PREFIX %>" suffix="<%= IConstants.ORG_SITE_URL %>" label="custom-url" cssClass="width-80"/>
			</aui:column>	
		</aui:row>	
	</c:if>

	<aui:input name="" type="checkbox" required="<%= true %>" label="i-agree-to-terms-and-conditions" showRequiredLabel="<%= false %>"/>
	<aui:button type="submit" value="Register" />	
</aui:form>

<aui:script>
	function emailNotExists() {
		var frm = document.<portlet:namespace/>fm;
		var ele = frm.<portlet:namespace/>emailAddress;
	
		var ajaxURL = Liferay.PortletURL.createResourceURL();
		ajaxURL.setPortletId('register_WAR_educoartportlet');
		ajaxURL.setParameter('<%= Constants.CMD %>', '<%= IConstants.CMD_CHECK_DUPLICATE %>');
		ajaxURL.setParameter('emailAddress', ele.value);
		ajaxURL.setWindowState('<%= LiferayWindowState.EXCLUSIVE.toString() %>');
		
		var notExists = true;
		AUI().io.request('<%= themeDisplay.getURLPortal() %>' + ajaxURL, {
			sync: true,
			on: {
				success: function() {
					notExists = (!(eval(this.get('responseData'))));
				}
			}
		});
		
		return notExists;
	}
	
	$(function() {
		var maxDate = new Date();
		var minDate = new Date(maxDate.getFullYear()-70, 0, 0);
		
		$('#<portlet:namespace/>dateOfBirth').datepicker({minDate: minDate, maxDate: maxDate});
		
		<c:if test="<%= org %>">
			$('#<portlet:namespace/>organizationName').keyup(function() {
				var value = this.value.replace(/\s+/g, '-').toLowerCase();
			    $('#<portlet:namespace/>subdomain').val(value);
			});
		</c:if>
	});
</aui:script>